import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import java.io.*;

public class FindAndReplace extends JDialog {
    private WDTMain frame;
    private JLabel searchTag = new JLabel("搜索内容：");
    private JTextField searchContent = new JTextField(25);
    private JLabel replaceTag = new JLabel("替换内容：");
    private JTextField replaceContent = new JTextField(25);
    private JButton findUp = new JButton("查找上一个");
    private JButton findDown = new JButton("查找下一个");
    private JLabel doReplaceText = new JLabel("查找时同时进行替换");
    private JCheckBox doReplace = new JCheckBox();
    private JButton replaceAll = new JButton("替换全部");
    private Container c = getContentPane();
    public FindAndReplace(WDTMain frame) {
        super(frame);
        this.frame = frame;
        c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
        JPanel panelUp = new JPanel();
        panelUp.setLayout(new FlowLayout());
        panelUp.add(searchTag);
        panelUp.add(searchContent);
        panelUp.add(findUp);
        c.add(panelUp);
        JPanel panelDown = new JPanel();
        panelDown.setLayout(new FlowLayout());
        panelDown.add(replaceTag);
        panelDown.add(replaceContent);
        panelDown.add(findDown);
        c.add(panelDown);
        JPanel panelCheckbox = new JPanel();
        panelCheckbox.setLayout(new FlowLayout());
        panelCheckbox.add(doReplaceText);
        panelCheckbox.add(doReplace);
        JPanel panelSouth = new JPanel();
        panelSouth.setLayout(new FlowLayout());
        panelSouth.add(panelCheckbox);
        panelSouth.add(replaceAll);
        c.add(panelSouth);
        findUp.setEnabled(false);
        findDown.setEnabled(false);
        replaceContent.setEditable(false);
        replaceAll.setEnabled(false);
        searchContent.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                findUp.setEnabled(searchContent.getText().length() != 0);
                findDown.setEnabled(searchContent.getText().length() != 0);
            }
            public void removeUpdate(DocumentEvent e) {
                findUp.setEnabled(searchContent.getText().length() != 0);
                findDown.setEnabled(searchContent.getText().length() != 0);
            }
            public void insertUpdate(DocumentEvent e) {
                findUp.setEnabled(searchContent.getText().length() != 0);
                findDown.setEnabled(searchContent.getText().length() != 0);
            }
        });
        doReplace.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                replaceContent.setEditable(doReplace.isSelected());
                replaceAll.setEnabled(doReplace.isSelected());
            }
        });
        findUp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CodeEdit ta = (CodeEdit) frame.getTabPane().getSelectedComponent();
                String searchText = searchContent.getText();
                String fullText = ta.getText();
                int start, end;
                if (ta.getSelectedText() == null) {
                    start = fullText.lastIndexOf(searchText, ta.getCaretPosition() - 1);
                } else {
                    start = fullText.lastIndexOf(searchText, ta.getCaretPosition() - ta.getSelectedText().length() - 1);
                }
                end = start + searchText.length();
                if (start > -1) {
                    ta.setCaretPosition(start);
                    ta.select(start, end);
                    ta.requestFocus();
                    if (doReplace.isSelected()) ta.replaceSelection(replaceContent.getText());
                } else {
                    JOptionPane.showMessageDialog(null, "再往上已经没有要找的内容了哦", "查找与替换", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        findDown.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CodeEdit ta = (CodeEdit) frame.getTabPane().getSelectedComponent();
                String searchText = searchContent.getText();
                String fullText = ta.getText();
                int start, end;
                if (ta.getSelectedText() == null) {
                    start = fullText.indexOf(searchText, ta.getCaretPosition());
                } else {
                    start = fullText.indexOf(searchText, ta.getCaretPosition() - ta.getSelectedText().length() + 1);
                }
                System.out.println(ta.getCaretPosition());
                end = start + searchText.length();
                if (start > -1) {
                    ta.setCaretPosition(start);
                    ta.select(start, end);
                    ta.requestFocus();
                    if (doReplace.isSelected()) ta.replaceSelection(replaceContent.getText());
                } else {
                    JOptionPane.showMessageDialog(null, "再往下已经没有要找的内容了哦", "查找与替换", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        replaceAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CodeEdit ta = (CodeEdit) frame.getTabPane().getSelectedComponent();
                if (ta.getText().indexOf(searchContent.getText()) >= 0) ta.setText(ta.getText().replaceAll(searchContent.getText(), replaceContent.getText()));
                else {
                    JOptionPane.showMessageDialog(null, "找不到要替换的文本", "查找与替换", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
    }
    void showup() {
        setSize(500, 140);
        setVisible(true);
        setDefaultCloseOperation(2);
        setResizable(false);
        setTitle("查找与替换");
    }
}